/**
 * Description: 管理员管理
 *
 * @author      Eden
 * @datetime    2016/11/28 16:37
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

(function () {
    
    "use strict";

    var api = {
        roleListUrl: "/admin/role/get",
        checkMemberUrl: '/admin/role/check',
        removeUrl: "/admin/role/remove"
    };

    sm.controller('admin-admin-role-index', ['$scope', '$http', 'Modal', function ($scope, $http, Modal) {
        $scope.roles = [];
        $scope.getLists = function () {
            $http.get(api.roleListUrl).success(function (res) {
                if (res.state) {
                    $scope.roles = res.data;
                }
            });
        };

        $scope.getLists();

        $scope.remove = function (roleId) {
            if (roleId == 0) {
                return false;
            }

            $http.get(api.checkMemberUrl, {params:{roleId: roleId}}).success(function (res) {
                if (res.state) {
                    Modal.alert({
                        title: "操作提示",
                        content:"您确定要删除该角色吗?",
                        buttons:[{
                            name: "取消"
                        }, {
                            name: "确认删除",
                            event: function () {
                                $scope.deleteItem(roleId);
                            }
                        }]
                    });
                } else {
                    Modal.error({
                        title: "操作提醒",
                        content: "该角色包含后台用户或发生错误,无法删除"
                    });
                }
            });
        };

        $scope.deleteItem = function (roleId) {
            $http.post(api.removeUrl, {
                roleId: roleId
            }).success(function (res) {
                if (res.state) {
                    Modal.alert({'title': "删除成功", buttons:[{
                        name: "确定",
                        event: function () {
                            location.href = "/admin/role/index";
                        }
                    }]});
                } else {
                    Modal.error('删除失败');
                }
            });
        };

    }]);

})();